<template>
  <div class="page">
	<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">

						<div class="layui-card-body">
							<div class="layui-form" lay-filter="">

								<div class="layui-form-item">
									<div class="layui-inline">
									    <label class="layui-form-label">等级名称</label>
									    <div class="layui-input-inline" style="width: 380px;">
									      <input type="text" name="name" placeholder="等级名称(限10字)"  v-model="itemData.name" autocomplete="off" class="layui-input a" maxlength="10">									    
									    </div>
								  </div>
								</div>
								<div class="layui-form-item">									
								    <label class="layui-form-label">会员级别 </label>
								    <div class="layui-input-inline" style="width: 380px;">
								    <select name="memberGrade" lay-filter="memberGrade" v-model="itemData.memberGrade">	  									    																		      
								        <option value='ZERO'>非会员</option>
								        <option value='ONE'>1级</option>
								        <option value='TWO'>2级</option>
								        <option value='THREE'>3级</option>
								        <option value='FOUR'>4级</option>
								        <option value='FIVE'>5级</option>
								        <option value='SIX'>6级</option>
								        <option value='SEVEN'>7级</option>
								        <option value='EIGHT'>8级</option>
								        <option value='NINE'>9级</option>
								        <option value='TEN'>10级</option>
								        <option value='ELEVEN'>11级</option>
								        <option value='TWELVE'>12级</option>
								        <option value='THIRTEEN'>13级</option>
								        <option value='FOURTEEN'>14级</option>
								        <option value='FIFTEEN'>15级</option>
								      </select>  
								    </div>
								 
								</div>
								<div class="layui-form-item">
									<div class="layui-inline">
									    <label class="layui-form-label">会员价格</label>
									    <div class="layui-input-inline" style="width: 380px;">
										     <input type="number" name="price" placeholder="会员价格(限5位)"  v-model="itemData.price" autocomplete="off" class="layui-input a"  maxlength="5"  oninput='this.value=this.value.replace(/^[0]+[0-9]*$/gi,"")'>									    
										  </div> 
								  </div>
								</div>
						    
						    <div class="layui-form-item">
									<div class="layui-block">
									    <label class="layui-form-label">会员折扣</label>
									    <div class="layui-input-inline" style="width: 380px;">
										     <input type="number" name="discount" placeholder="会员折扣例：75"  v-model="itemData.discount"  autocomplete="off" lay-verify="required" class="layui-input a"  maxlength="3" onkeyup="this.value=this.value.replace(/\D|^0/g,'')" >									    
										  </div> 
								  </div>
								</div>
								<div class="layui-form-item">
									<div class="layui-input-block">
										<button class="layui-btn layui-btn-normal" lay-submit lay-filter="setmyinfo" @click="confirmOk">确认</button>										
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
  </div>
</template>
<script>

export default {
  name: 'leveladd',
  data () {
    return {
    	id:'',
		itemData:{
			memberGrade:'0',
		    price:''
		},    	
    }
  },
  mounted(){
    var _this = this;     
  	window.getdates = this.getdates;  	
  },
  methods: {
		getdates:function(data){
      var _this=this;
      this.id=data.id;
      var url="/shop/bapi/v1.9/levels/"+data.id;
      ajaxRequest(url, 'GET', 'json','', function(ret, err){
		_this.itemData=ret.data;   
		_this.itemData.price = ret.data.price/100;          
      })
		},		
  	confirmOk:function(){
  		var _this = this;
			var len = $(".a").length;
			for(var i = 0; i < len; i++) {
				if($(".a").eq(i).val() == ''||$(".a").eq(i).val()<=0) {
					var msg = $(".a").eq(i).attr("placeholder");
					layer.msg('请输入正确的'+msg);
					return;
				}
			}
			if(_this.itemData.price<0){
        layer.msg("请输入价格不能小于0")
        return false;
      }
      if(_this.itemData.discount<0){
        layer.msg("请输入折扣不能小于0")
        return false;
      }
			if(_this.id){
			  var hat='PUT'
				var dates={
					 name:_this.itemData.name,
		       memberGrade:_this.itemData.memberGrade,
		       price:_this.itemData.price*100,
		       discount:_this.itemData.discount,
					 id:_this.id,
				}
			}else{
				var hat='POST'
				var dates={
					 name:_this.itemData.name,
		       memberGrade:_this.itemData.memberGrade,
		       price:_this.itemData.price*100,
		       discount:_this.itemData.discount,
				}
			}
			var url="/shop/bapi/v1.9/levels" 
	    ajaxRequest(url,hat,'json',dates,function(ret,err){
			  layer.msg("操作成功");
			  setTimeout(function(){
			  	var index = parent.layer.getFrameIndex(window.name);
			    parent.layer.close(index);
		      parent.location.reload();
	      },1000)
			})
  	}
  },
  updated:function(){
  	var _this=this;
		layui.use('form', function(){
		  var form = layui.form;
		  form.render();
		  form.on('select(memberGrade)', function(data){
				 	_this.itemData.memberGrade=data.value;
				 //	var name=data.elem.getAttribute('name');				 	
			});

		});

  },
}
</script>
<style scoped>
</style>
